const app = getApp()

Component({
	/**
	 * 组件的属性列表
	 */
	options: {
		multipleSlots: true, // 在组件定义时的选项中启用多slot支持
		addGlobalClass: true,
	},
	behaviors: ['wx://form-field'],
	/**
	 * 页面传参
	 */
	properties: {
		form_info: {
			type: Object,
			value: {}
		},
	},
	/*
	observers: {
		'form_info'(form_info) {
			var that = this
			that.setData({
				lists: form_info
			})
			if (form_info.value) {
				that.setData({
					val: form_info.value,
					value: form_info.value,
					fileList: form_info.value,
					min: form_info.value.length
				})
			}
		},

	},
	*/
	/**
	 * 组件的初始数据
	 */
	data: {
		fileList: [],
		lists: '',
		max: '9',
		min: '0',
		userInfo: '',
		fullurl: ''
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		afterRead(event) {
			//console.log(event.detail);
			let that = this;
			var userInfo = wx.getStorageSync('userInfo');
			//console.log(userInfo)
			const {
				file
			} = event.detail;
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			// 上传完成需要更新 fileList
			const {
				fileList = []
			} = that.data;


			
			file.forEach(function(e) {
				//console.log(e)
				wx.uploadFile({
					url: 'https://service.zn-care.com/api/uploadImage',
					//url: 'https://zl.mu6.cn/api/multipleUpload',
					filePath: e.url,
					name: 'file',
					formData: {
						"files": e.url,
					},
					success: function(res) {
						console.log(res)
						if(res.statusCode == 200){
							var data = JSON.parse(res.data);
							//console.log(data)
							//var fullurl = 'https://zl.mu6.cn' + data.data
							var fullurl = data.data
							//var fullurl = app.globalData.hosts + data.data.path;
							console.log(fullurl)
							
							that.setData({
								fullurl: fullurl
							})
							fileList.push({
								//...file,
								url: fullurl
							});
							that.setData({
								fileList: fileList,
								min: fileList.length,
								value: fileList
							});
							var val = fileList; //通过这个传递数据
							var myEventDetail = {
								val: val
							}
							that.triggerEvent('myevent', myEventDetail);
						}else{
							var data = JSON.parse(res.data);
							wx.showToast({
								title: data.msg,
								icon: 'none',
								duration: 2000
							})
						}

						
						
					},
					error: function(err) {
						console.log(err)
					}

				});
				
				
			});
			

		},
		deleteImage(event) {
			console.log(event);
			let that = this;
			const {
				fileList = []
			} = that.data;
			fileList.splice(event.detail.index, 1)

			that.setData({
				fileList: fileList,
				value: fileList,
				min: fileList.length
			});
			console.log(fileList)
			var val = fileList; //通过这个传递数据
			var myEventDetail = {
				val: val,
				//type: 2
			}
			that.triggerEvent('myevent', myEventDetail);
		},
	}
})
